<?php

extract(extractPost(), EXTR_PREFIX_ALL, 'u');

if(isset($u_user_id) && isset($u_group_id)){

    $result = array();

    $group = R::load('group', $u_group_id );
    $user = R::load('user', $u_user_id);

    if($group->id == 0)
    {
        jsonErrorDie("Group does not exists. Please use another group id.");
    }
    if($user->id == 0)
    {
        jsonErrorDie("User does not exists. Please use another user id.");
    }


    if(!isInBeans(3, $user->sharedRoleList))
        jsonErrorDie("User has no rights to become product owner");

    $a = R::findOne( 'gactivity', 'user_id = ? AND group_id = ? ORDER BY id DESC', [$user->id, $group->id]);
    if ($a != 0)
    {
        $a->dateTo = R::isoDateTime();
        $a = R::store( $a );
    }

    $group->productOwner = $user->id;
    R::store($group);

    jsonSuccessDie("message", "Successfuly changed product owner");
}

jsonErrorDie("Problem occured because user od group does not exists");
